package com.yeyks.controller.other;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yeyks.common.ajax.AjaxResponseBody;
import com.yeyks.common.exception.ServiceException;
import com.yeyks.config.dal.domain.RechargeConfig;
import com.yeyks.commonReference.service.config.RechargeConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 充值配置表 前端控制器
 * </p>
 *
 * @author yun
 * @since 2019-06-05
 */
@Api(tags = "充值配置", description = "充值配置")
@RestController
@RequestMapping(value = "/rechargeConfigEntity", method = RequestMethod.POST)
public class RechargeConfigController {

    @Autowired
    private RechargeConfigService rechargeConfigService;

    @ApiOperation(value = "查询", notes = "查询")
    @RequestMapping("/select")
    public AjaxResponseBody select(Integer status) {
        QueryWrapper<RechargeConfig> queryWrapper = new QueryWrapper<RechargeConfig>()
                .eq(RechargeConfig.TYPE, 2);
        if (null != status) {
            queryWrapper.eq(RechargeConfig.STATUS, status);
        }
        List<RechargeConfig> list = rechargeConfigService.list(queryWrapper);
        return AjaxResponseBody.success(list);
    }

    @ApiOperation(value = "修改状态", notes = "修改状态")
    @RequestMapping("/updateStatus")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "id", dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "status", value = "状态 默认0未生效，1生效", dataType = "Integer"),
    })
    public AjaxResponseBody updateStatus(Integer id, Integer status) {
        if (null == id || id < 1) {
            throw new ServiceException("id错误");
        }
        if (null == status) {
            throw new ServiceException("状态错误");
        }
        RechargeConfig rechargeConfig = rechargeConfigService.getById(id);
        if (null == rechargeConfig) {
            throw new ServiceException("充值配置不存在");
        }
        rechargeConfig.setStatus(status);
        boolean update = rechargeConfigService.updateById(rechargeConfig);
        if (!update) {
            throw new ServiceException("修改出错");
        }
        return AjaxResponseBody.success("修改成功");
    }


    @ApiOperation(value = "添加", notes = "添加")
    @RequestMapping("/insert")
    public AjaxResponseBody insert(RechargeConfig rechargeConfig) {
        if (null == rechargeConfig) {
            throw new ServiceException("添加的数据不存在");
        }
        if (null == rechargeConfig.getTitle()) {
            throw new ServiceException("title不存在");
        }
        if (null == rechargeConfig.getPayAmount()) {
            throw new ServiceException("payAmount不存在");
        }
        if (null == rechargeConfig.getGiveAmount()) {
            throw new ServiceException("giveAmount不存在");
        }
        rechargeConfig.setId(null).setStatus(1).setType(2).setUserType(rechargeConfig.getUserType()).setCreateTime(null).setUpdateTime(null);
        boolean save = rechargeConfigService.save(rechargeConfig);
        if (!save) {
            throw new ServiceException("添加出错");
        }
        return AjaxResponseBody.success("添加成功");
    }


    @ApiOperation(value = "修改", notes = "修改")
    @RequestMapping("/update")
    public AjaxResponseBody update(RechargeConfig rechargeConfigParam) {
        if (null == rechargeConfigParam) {
            throw new ServiceException("修改的数据不存在");
        }
        if (null == rechargeConfigParam.getId()) {
            throw new ServiceException("id不存在");
        }
        if (null == rechargeConfigParam.getTitle()) {
            throw new ServiceException("title不存在");
        }
        if (null == rechargeConfigParam.getPayAmount()) {
            throw new ServiceException("payAmount不存在");
        }
        if (null == rechargeConfigParam.getGiveAmount()) {
            throw new ServiceException("giveAmount不存在");
        }
        RechargeConfig rechargeConfig = rechargeConfigService.getById(rechargeConfigParam.getId());
        rechargeConfig.setTitle(rechargeConfigParam.getTitle())
                .setPayAmount(rechargeConfigParam.getPayAmount())
                .setGiveAmount(rechargeConfigParam.getGiveAmount())
                .setUpdateTime(null);
        boolean save = rechargeConfigService.updateById(rechargeConfig);
        if (!save) {
            throw new ServiceException("修改出错");
        }
        return AjaxResponseBody.success("修改成功");
    }

}
